//
//  DCIMImagePicker.m
//  xmppBase
//
//  Created by striby on 16/1/7.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMImagePicker.h"
#import <AVFoundation/AVFoundation.h>
@implementation DCIMImagePicker

-(void)showActionView
{
    
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0)
        
    {
        UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction * imageLibrary = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.pickerType = imagePickerTypeLibrary;
            [self startImagePicker];
        }];
        
        UIAlertAction * photoLibrary = [UIAlertAction actionWithTitle:@"从相机选择" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            self.pickerType = imagePickerTypeCamera;
            [self startImagePicker];
        }];
        
        
        
        
    UIAlertController * action = [UIAlertController alertControllerWithTitle:@"图像选择" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
        
        [action addAction:imageLibrary];
        [action addAction:photoLibrary];
        [action addAction:cancel];
        
        
        if (isIPHONE) {
            [[[UIApplication sharedApplication].delegate window].rootViewController presentViewController:action animated:YES completion:nil];
        }
        else
        {
            
            
//            action.popoverPresentationController.sourceRect = self.delegate.view.bounds;
//            
//            action.popoverPresentationController.sourceView = self.delegate.view;
//            
//            [self.delegate presentViewController:action animated:YES completion:nil];
//            ;
        }
     

        
        
    }
    else
    {
        
    }
}


-(void)startImagePicker
{
    if (self.pickerType==imagePickerTypeCamera) {
        
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self.delegate;
        _picker.allowsEditing = YES;
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        
        UINavigationController * na = [[UIApplication sharedApplication].delegate window].rootViewController;
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
            na.topViewController.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        }
        
//        int64_t delayInSeconds = 1.0;
//        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [na.topViewController presentViewController:_picker animated:NO completion:nil];
//        });
        
        
     
        
    }
    else
    {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            _picker = [[UIImagePickerController alloc] init];
            _picker.delegate = self.delegate;
            _picker.allowsEditing = YES;
            _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            [[[UIApplication sharedApplication].delegate window].rootViewController presentViewController:_picker animated:YES completion:^{
//                
//            }];
            
            UINavigationController * na = [[UIApplication sharedApplication].delegate window].rootViewController;
            [na.topViewController presentViewController:_picker animated:YES completion:^{
                
            }];
        }
        else
        {
            [self showAlertForText:@"好可惜没有相册权限"];
        }
    }
    
}

-(void)showAlertForText:(NSString*)text
{
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0)
    
    {
        UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:text preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:cancel];
        
        [[[UIApplication sharedApplication].delegate window].rootViewController presentViewController:alert animated:YES completion:nil];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:text delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
        [alert show];
    }
   
}


@end
